#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

//const char *myfile="/home/hbr/linux/process/6.replace/mycmd";
const char *myfile="./mycmd";

int main()
{
    pid_t id=fork();
    if(id==0)
    {
        printf("子进程开始运行,pid:%d\n",getpid());
        sleep(2);
       char *const _argv[10]={
           (char*)"ls",
           (char*)"-l",
           (char*)"-a",
           (char*)"-i",
           NULL };
        //execl("/usr/bin/ls","ls","-a","-l",NULL);
        //execv("/usr/bin/ls",_argv);
        //execlp("ls","ls","-a","-l","-i",NULL);
        //ecvp("ls",_argv);
        
        char *const _env[10]={
            (char*)"MY_VAL=1024",NULL};
        
        //execl(myfile,"mycmd","-b",NULL);
        //execle(myfile,"mycmd","-a",NULL,_env);
        

        char *const _mycmd[10]={
            (char*)"mycmd",
            (char*)"-a"};
        execve(myfile,_mycmd,_env);

        exit(1);
    }
    else
    {
        printf("父进程开始运行,pid:%d\n",getpid());
        int status=0;
        pid_t id=waitpid(-1,&status,0);
        if(id>0)
        {
            printf("wait success,exit code:%d\n",WEXITSTATUS(status));
        }
    }
    return 0;
}
